const redis = require("redis");
const { REDIS_CONF } = require("../conf/db");
const redisClient = redis.createClient(REDIS_CONF.port, REDIS_CONF.host);

redisClient.on("error", (err) => {
  console.error(err);
});
function set(key, val) {
  redisClient.set(
    key,
    typeof val === "object" ? JSON.stringify(val) : val,
    redis.print
  );
}
function get(key) {
  return new Promise((resolve, reject) => {
    redisClient.get(key, (err, val) => {
      if (err) return reject(err);
      try {
        resolve(JSON.parse(val));
      } catch (e) {
        resolve(val);
      }
    });
  });
}
module.exports = {
  set,
  get,
};
